# FeedEntryScreen - This screen displayes one entry of some RSS feed
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

# Widgets
from frontend.gui.widgets.menu import Menu
from frontend.gui.widgets.selector import Selector
from frontend.gui.widgets.menu_item import MenuItem
from frontend.gui.widgets.scroll_area import ScrollArea
from frontend.gui.widgets.list_indicator import ListIndicator

from frontend.user_event import UserEvent
from utils.feed_utils import FeedEntryParser
from frontend.medialibrary.feeds import Feed
from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

class FeedEntryScreen(Screen):
    """
    Feed entry Screen
    
    This screen displays one feed entry.
    """
    
    def __init__(self, ui, frontend, data):
        """
        Initialize screen
        
        @param frontend: FrontendClient object
        @param theme: Theme object
        @param data: List [Feed object, Entry object]
        """
        self.group = clutter.Group()
        Screen.__init__(self, ui.STAGE_WIDTH, ui.STAGE_HEIGHT)
        
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.feed = data[0]
        self.entry = data[1]
        
        # Screen Title (Displayed at the bottom left corner)
        screen_title = clutter.Label()
        screen_title.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.13)) + "px")
        screen_title.set_color(self.theme.getColor("screentitle"))
        screen_title.set_use_markup(True)
        screen_title.set_text(FeedEntryParser().convert(self.feed.get_title()))
        screen_title.set_name("screen_title")
        screen_title.set_position(self.ui.x(0), self.ui.y(0.87))
        screen_title.show()
        self.group.add(screen_title)
        
        # RSS Icon
        rss = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("rss_icon"))
        icon = clutter.Texture()
        icon.set_pixbuf(rss)
        icon.set_position(self.frontend.getUserInterface().x(0.05), 
                          self.frontend.getUserInterface().y(0.07))
        icon.show()
        self.group.add(icon)
        
        # Entry Title
        title = clutter.Label()
        title.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0625)) + "px")
        title.set_color(self.theme.getColor("title"))
        title.set_use_markup(True)
        title.set_text(FeedEntryParser().convert(self.feed.get_title()))
        title.set_width(self.ui.x(0.5124))
        title.set_line_wrap(False)
        title.set_ellipsize(pango.ELLIPSIZE_END)
        title.set_position(self.ui.x(0.13), self.ui.y(0.06))
        title.show()
        self.group.add(title)
        
        # RSS Feed descrption
        full_text = self.entry.get_title()
        try:
            sub_title = full_text[:full_text.index('\n')]
        except ValueError:
            sub_title = full_text
        desc = clutter.Label()
        desc.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0365)) + "px")
        desc.set_color(self.theme.getColor("subtitle"))
        desc.set_use_markup(True)
        desc.set_text(FeedEntryParser().convert(sub_title))
        desc.set_line_wrap(False)
        desc.set_ellipsize(pango.ELLIPSIZE_END)
        desc.set_width(self.ui.x(0.5124))
        desc.set_position(self.ui.x(0.13), self.ui.y(0.14))
        desc.show()
        self.group.add(desc)
        
        # Last update
        date_text = self.entry.get_time()[:5]  + " - " +  self.entry.get_date()
        date = clutter.Label()
        date.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0313)) + "px")
        date.set_color(self.theme.getColor("subtitle"))
        date.set_text(date_text)
        date.set_position(self.ui.x(0.78), self.ui.y(0.14))
        date.show()
        self.group.add(date)
        
        # Last update title
        update = clutter.Label()
        update.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0313)) + "px")
        update.set_color(self.theme.getColor("title"))
        update.set_text("Updated")
        update.set_position(self.ui.x(0.78), self.ui.y(0.1))
        update.show()
        self.group.add(update)
        
        # Entry descrption
        if len(self.entry.get_description().strip()) == 0:
            entry_text = clutter.Label()
            entry_text.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.04167)) + "px")
            entry_text.set_color(self.theme.getColor("text"))
            entry_text.set_text("Entry has no content")
            x = int((self.ui.STAGE_WIDTH - entry_text.get_width()) / 2)
            entry_text.set_position(x, self.ui.y(0.48))
            entry_text.show()
            self.group.add(entry_text)
        else:
            entry_text = clutter.Label()
            #use pango markup
            entry_text.set_use_markup(True)
            entry_text.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0365)) + "px")
            entry_text.set_color(self.theme.getColor("text"))
            #convert entry to pango markup and send to text entry
            entry_text.set_text(FeedEntryParser().convert(self.entry.get_description()))
            entry_text.set_size(self.ui.x(0.5857), self.ui.y(0.5208))

            self.scroll_area = ScrollArea(self.ui.x(0.5857), self.ui.y(0.4948), self.theme)
            self.scroll_area.set_step_size(self.ui.y(0.03906))
            self.scroll_area.set_content(entry_text)
            self.scroll_area.set_position(self.ui.x(0.2072), self.ui.y(0.3))
            self.scroll_area.show()
            self.scroll_area.set_active(True)
            self.group.add(self.scroll_area)
        
        self.group.show()
        self.frontend.getUserInterface().getStage().add(self.group)
        
        #We've got to the end without any problems so lets set the entry as read
        self.entry.mark_as_read()
    
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """
        Get screen type.
        @return: Integer
        """
        return Screen.NORMAL
    
    def get_name(self):
        """
        Get screen name
        @return: String
        """
        return "EntryFeedScreen"
    
    def handle_user_event(self, event):   
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        """
        type = event.get_type()
        
        if type == UserEvent.NAVIGATE_UP:
            self.scroll_area.scroll_up()
                
        elif type == UserEvent.NAVIGATE_DOWN:
            self.scroll_area.scroll_down()
            
        elif type == UserEvent.NAVIGATE_FIRST_PAGE:
            self.scroll_area.scroll_to_top()
            
        elif type == UserEvent.NAVIGATE_PREVIOUS_PAGE:
            self.scroll_area.scroll_to_bottom()
            
        elif type == UserEvent.NAVIGATE_PREVIOUS_PAGE:
            self.scroll_area.scroll_page_up()
            
        elif type == UserEvent.NAVIGATE_NEXT_PAGE:
            self.scroll_area.scroll_page_down()
